package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.models.attribute.AttributeUtil;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.item.Item;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;
import com.keyroy.game.puzzle_rpg.views.tables.CostTableView;
import com.keyroy.game.puzzle_rpg.views.tables.EffectTableView;
import com.keyroy.game.puzzle_rpg.views.tables.RequireTableView;

public class ItemEditor extends Shell {
	private Text name;
	private Text icon;
	private Text info;
	private EffectTableView effect_table_view;
	private CostTableView cost_table_view;
	private IconView canvas;
	private Combo type;
	private RequireTableView require_table_view;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			ItemEditor shell = new ItemEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @wbp.parser.constructor
	 */
	public ItemEditor(Display display, final EventListener<Item> listener) {
		this(display, null, listener);
	}

	public ItemEditor(Display display, int index, final EventListener<Item> listener) throws Exception {
		this(display, AttributeUtil.getItemClass(index).newInstance(), listener);
	}

	public ItemEditor(Display display, Item item, final EventListener<Item> listener) {
		super(display, SWT.DIALOG_TRIM);

		canvas = new IconView(this);
		canvas.setBounds(10, 10, 100, 100);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(116, 13, 35, 17);
		lblNewLabel.setText("\u540D\u79F0 : ");

		Label label = new Label(this, SWT.NONE);
		label.setText("\u56FE\u6807 : ");
		label.setBounds(116, 47, 35, 17);

		name = new Text(this, SWT.BORDER);
		name.setBounds(157, 10, 277, 23);

		icon = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		icon.setBounds(157, 44, 191, 23);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					icon.setText(path);
				}
				canvas.setImage(path);
			}
		});
		btnNewButton.setBounds(354, 42, 80, 27);
		btnNewButton.setText("\u9009\u62E9\u6587\u4EF6");

		TabFolder tabFolder = new TabFolder(this, SWT.NONE);
		tabFolder.setBounds(10, 116, 424, 263);

		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("\u6548\u679C");

		effect_table_view = new EffectTableView(tabFolder);
		tabItem.setControl(effect_table_view);

		TabItem tableItem22 = new TabItem(tabFolder, SWT.NONE);
		tableItem22.setText("\u8981\u6C42");

		require_table_view = new RequireTableView(tabFolder);
		tableItem22.setControl(require_table_view);

		TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("\u6D88\u8017/\u5408\u6210");

		cost_table_view = new CostTableView(tabFolder);
		tabItem_1.setControl(cost_table_view);
		
		TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);
		tabItem_2.setText("\u7B80\u4ECB");
		
				info = new Text(tabFolder, SWT.BORDER | SWT.WRAP);
				tabItem_2.setControl(info);

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				if (listener != null) {
					Class<? extends Item> clazz = AttributeUtil.getItemClass(type.getSelectionIndex());
					try {
						Item item = clazz.newInstance();
						item.name = name.getText();
						item.icon = icon.getText();
						item.info = info.getText();
						item.effect = effect_table_view.getEffect();
						item.require = require_table_view.getRequire();
						item.cost = cost_table_view.getCost();
						listener.onEvent(item);
						dispose();
					} catch (Exception e) {
					}
				}
			}
		});
		btnNewButton_1.setBounds(354, 385, 80, 27);
		btnNewButton_1.setText("\u5B8C\u6210");

		Label lblNewLabel_2 = new Label(this, SWT.NONE);
		lblNewLabel_2.setBounds(10, 390, 35, 17);
		lblNewLabel_2.setText("\u7C7B\u578B : ");

		type = new Combo(this, SWT.READ_ONLY);
		type.setBounds(51, 387, 297, 25);
		type.select(0);

		type.setItems(AttributeUtil.getItemTypes());
		createContents();

		set(item);
	}

	public void set(Item item) {
		if (item != null) {
			name.setText(stringOf(item.name));
			icon.setText(stringOf(item.icon));
			canvas.setImage(item.icon);
			info.setText(stringOf(item.info));
			effect_table_view.set(item.effect);
			cost_table_view.set(item.cost);
			require_table_view.set(item.require);
			type.select(AttributeUtil.getItemIndex(item.getClass()));
		}
	}

	private final String stringOf(String str) {
		if (str != null) {
			return str;
		} else {
			return "";
		}
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u9053\u5177\u7F16\u8F91\u5668");
		setSize(450, 450);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public IconView getCanvas() {
		return canvas;
	}

	public Composite getComposite() {
		return require_table_view;
	}
}
